<template>
  <el-dialog :title="title" :visible="visible" @close="close">
    <el-form
      ref="form"
      :model="entityData"
      :rules="rules"
      label-width="80px"
      label-position="right"
      style="width:90%;margin:0px auto;"
    >
      <!--表单区域 -->
      <el-form-item label="任务" prop="jobId">
        <el-select v-model="entityData.jobId" placeholder="请选择" @change="handleChange">
          <el-option v-for="item in jobList" :key="item.id" :label="item.name" :value="item.id" />
        </el-select>
      </el-form-item>

      <el-form-item label="调度名称" prop="name">
        <el-input v-model="entityData.name" />
      </el-form-item>
      <el-form-item label="调度配置" prop="cronExpression">
        <cron v-model="entityData.cronExpression" input-style="width:320px;" />
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input v-model="entityData.remark" type="textarea" />
      </el-form-item>
      <el-form-item label="排序号">
        <el-input v-model="entityData.orderNo" />
      </el-form-item>
      <el-form-item label="任务参数">
        <div style="margin-top:10px;">
          <div class="clearfix" />
          <el-table
            :data="entityData.paramList"
            highlight-current-row
            style="margin-top:5px;"
            border
          >
            <el-table-column type="index" label="序号" width="50" />
            <el-table-column width="160px">
              <template slot="header">编码</template>
              <template slot-scope="scope">
                <!--TODO:禁用影响用户体验,需调整为只读并去除边框-->
                <el-input v-model="scope.row.paramCode" disabled />
              </template>
            </el-table-column>
            <el-table-column>
              <template slot="header">值</template>
              <template slot-scope="scope">
                <el-input v-model="scope.row.paramValue" />
              </template>
            </el-table-column>
            <el-table-column>
              <template slot="header">描述</template>
              <template slot-scope="scope">
                <el-input v-model="scope.row.paramDescription" />
              </template>
            </el-table-column>
            <el-table-column label="操作" width="50">
              <template slot-scope="scope">
                <i class="delete el-icon-delete" @click="removeParam(scope.$index)" />
              </template>
            </el-table-column>
          </el-table>
        </div>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button v-show="saveButtonVisible" type="primary" @click="save">保存</el-button>
      <el-button @click="close">关闭</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { detailMixin } from '@/mixin/detailMixin'
import Cron from '@/components/CronExpression'
const MODULE_CODE = 'scheduler'
const ENTITY_TYPE = 'schedulerJob'
export default {
  name: ENTITY_TYPE + '-detail',
  components: {
    Cron
  }, mixins: [detailMixin],
  data() {
    return {
      entityType: ENTITY_TYPE,
      moduleCode: MODULE_CODE,
      // eslint-disable-next-line no-eval
      api: eval('this.$api.' + MODULE_CODE + '.' + ENTITY_TYPE),
      pageCode: MODULE_CODE + ':' + ENTITY_TYPE + ':',
      entityData: {

      },
      rules: {
        jobId: [
          { required: true, message: '请选择任务', trigger: 'blur' }
        ],
        name: [
          { required: true, message: '请输入名称', trigger: 'blur' }
        ],
        cronExpression: [
          { required: true, message: '请进行调度配置', trigger: 'blur' }
        ]
      },
      currentRow: {},
      jobList: []
    }
  },
  created: function () {
    // 加载任务列表
    this.$api.scheduler.job.list()
      .then(res => {
        this.jobList = res.data
      })
  },
  methods: {
    // 参数相关处理
    removeParam(index) {
      this.entityData.paramList.splice(index, 1)
    },
    handleChange(value) {
      this.$api.scheduler.job.get(value)
        .then(res => {
          const job = res.data
          this.entityData.jobId = job.id
          this.entityData.jobName = job.name
          this.entityData.paramList = []
          job.paramList.forEach((item) => {
            this.entityData.paramList.push({
              paramCode: item.paramCode, paramValue: item.paramValue,
              paramDescription: item.paramValue
            })
          })
        })
    }
  }

}
</script>

<style lang="scss">
</style>
